package com.jb.repair.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by zxj on 2015/12/17.自动换行的linerlayout 并且每行最多显示7个
 */
public class AutoChangeLineLinerLayout extends ViewGroup{
   private final static String TAG = "AutoChangeLine";

    public AutoChangeLineLinerLayout(Context context) {
        super(context);
    }

    public AutoChangeLineLinerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoChangeLineLinerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public AutoChangeLineLinerLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

        private final static int VIEW_MARGIN = 10;

        @Override
        protected void onMeasure ( int widthMeasureSpec, int heightMeasureSpec) {


            Log.d(TAG, "widthMeasureSpec=" + widthMeasureSpec
                    + "heightMeasureSpec=" + heightMeasureSpec);

            int stages = 1;
            int stageHeight = 0;
            int stageWidth = 0;

            int wholeWidth = MeasureSpec.getSize(widthMeasureSpec);

            for (int i = 0; i < getChildCount(); i++) {
                final View child = getChildAt(i);
                // measure
                measureChild(child, widthMeasureSpec, heightMeasureSpec);
                stageWidth += (child.getMeasuredWidth() + VIEW_MARGIN);
                stageHeight = child.getMeasuredHeight();
                if (stageWidth >= wholeWidth) {
                    stages++;
                    //reset stageWidth
                    stageWidth = child.getMeasuredWidth();
                }

                Log.i(TAG, "i:" + i + ",wholeWidth:" + wholeWidth
                        + ",stageWidth:" + stageWidth+",stageHeight:"+stageHeight+",stage:"+stages);
            }

            Log.i(TAG, "stages:" + stages);

            int wholeHeight = (stageHeight + VIEW_MARGIN) * stages;

            // report this final dimension
            setMeasuredDimension(resolveSize(wholeWidth, widthMeasureSpec),
                    resolveSize(wholeHeight, heightMeasureSpec));

        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {

            final int count = getChildCount();
            int row = 0; // which row lay your view relative to parent
            int lengthX = l; // right position of child relative to parent
            int lengthY = t; // bottom position of child relative to parent
            int measuredWidth = this.getChildAt(0).getMeasuredWidth();
            int cc = 0;

            for (int i = 0; i < count; i++) {
                final View child = this.getChildAt(i);
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                int child_num = cc++;
                lengthX += width + VIEW_MARGIN;
                lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height;
                if(row == 0 && child_num >7 ){
                    lengthX = width + VIEW_MARGIN + l+measuredWidth+VIEW_MARGIN;
                    row++;
                    cc =0;
                    child_num = 0;
                    lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height;
                }
                if(child_num >= 6 && row >=1){
                    lengthX = width + VIEW_MARGIN + l+measuredWidth+VIEW_MARGIN;
                    row++;
                    cc =0;
                    child_num = 0;
                    lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height;
                }
                // if it cant't draw in a same line ,skip it to next line
                if (lengthX > r) {
                    lengthX = width + VIEW_MARGIN + l+measuredWidth+VIEW_MARGIN;
                    row++;
                    cc =0;
                    child_num = 0;
                    lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height;
                }
                child.layout(lengthX - width, lengthY - height, lengthX, lengthY);
            }

        }
}
